package com.ziyun.pop.mall.module.user.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.service.ucenter.api.UserTaxService;
import com.service.ucenter.model.tax.TaxParamDto;
import com.service.ucenter.model.tax.UserTaxVO;
import com.service.utils.push.umeng.JsonResult;
import com.utility.JsonUtils;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;

/**
 * 用户发票Controller
 * 
 * @author lvai 9.14
 */
@Controller
public class UserTaxController {
	private static Logger _log = LoggerFactory.getLogger(UserTaxController.class);
	private static final String MODEL = "/user/userTax";

	@Autowired
	private UserTaxService userTaxService;

	@Autowired
	private HttpRequestLocal httpRequestLocal;

	/**
	 * 获取发票信息列表
	 * 
	 * @return
	 * @throws IOException
	 */
	@GetMapping(MODEL + "/index.do")
	public ModelAndView index() throws IOException {
		ModelAndView view = new ModelAndView("/user/userTax/index.html");
		return view;
	}

	/**
	 * 新增/编辑发票
	 * 
	 * @param Date
	 * @return
	 */
	@PostMapping(MODEL + "/saveTax.json")
	@ResponseBody
	public JsonResult<UserTaxVO> saveTax(String taxData) {
		TaxParamDto taxParamDto = JsonUtils.string2Object(taxData, TaxParamDto.class);

		UserTaxVO tax = userTaxService.saveTax(taxParamDto).getData();
		return JsonResult.success(tax);
	}

	/**
	 * 获取用户发票列表信息
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/listUserTax.json")
	@ResponseBody
	public JsonResult<Map<String, Object>> listUserTax() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<UserTaxVO> listUserTax = userTaxService.listUserTax().getData();
		map.put("listUserTax", listUserTax);
		int taxCount;
		if (listUserTax != null && listUserTax.size() > 0) {
			taxCount = listUserTax.size();
		} else {
			taxCount = 0;
		}
		map.put("taxCount", taxCount);
		return JsonResult.success(map);
	}

	/**
	 * 删除发票信息
	 * 
	 * @param taxId
	 * @return
	 */
	@PostMapping(MODEL + "/delTax.json")
	@ResponseBody
	public JsonResult<?> delTax(Integer taxId) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("taxId", taxId);
		userTaxService.delTax(map);
		return JsonResult.success();
	}

	/**
	 * 根据发票Id获取发票信息
	 * 
	 * @param taxId
	 * @return
	 */
	@PostMapping(MODEL + "/getTax.json")
	@ResponseBody
	public JsonResult<?> getTax(Long taxId) {
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		UserTaxVO userTaxVO = userTaxService.getTax(userId, taxId).getData();
		return JsonResult.success(userTaxVO);
	}

}
